Zhang, Z., & Wang, L. (2009). Power analysis for growth curve models using SAS. Behavior Research Methods, 41(4), 1083-1094. Request a copy
/*Suppress the output and the log */
options nosource nonotes nosource2 nomprint;
/*Power analysis of growth curve models using SAS*/
/*CHANGE THE PARAMETERS HERE*/
*model parameters;
%LET MuL=10; *mean level/initial status;
%LET MuS=.1; *mean slope/rate of change;
%LET Sigma_e=1; *residual standard deviation;
%LET Sigma_L=2; *level standard deviation;
%LET Sigma_S=1; *slope standard deviation;
%LET rho=0; *correlation between levels and slopes;
%LET miss=0.1; *missing data rate, 0: no missing data;
%LET treatmentL=1; *group difference in the level
%LET treatmentS=1; *group difference in the rate of change
*power parameters;
%LET R=5000; *number of simulation replications;
%LET T=5; *number of measurement occasions;
%LET start=100; *the minimum sample size to consider;
%LET end=100; *the maximum sample size to consider;
%LET step=50; *the step between two sample sizes;
%LET df=1; *the difference in the numbers of parameters;
%LET seed=4321; *random number generator seed;
/*DO NOT CHANGE CODES BELOW UNLESS YOU KNOW WHAT YOU ARE DOING*/
/*The first Macro: LL*/
/*Calculate the chi-square difference of two nested growth curve models*/
%MACRO LL(N,T,seed);
DATA Sim_LinGM;
* set statistical parameters;
N = &N; seed = &seed;
* setup arrays so that we can include multiple variables for repeated measures;
ARRAY y_score{&T} y1-y&T;
ARRAY M{&T} m1-m&T;
m1=1;
* generate raw data with considering the missing data rate;
DO _N_ = 1 TO N;
x = RANBIN(seed,1,.5);
e_L=RANNOR(seed);
e_S=&rho*e_L+SQRT(1-&rho**2)*RANNOR(seed);
L_score=&MuL+&treatmentL*x+&Sigma_L*e_L;
S_score=&MuS+&treatmentS*x+&Sigma_S*e_S;
* include indicator variables to generate missing data;
DO t = 1 TO &T;
y_score{t} = L_score + (t-1) * S_score + &Sigma_e*RANNOR(seed);
END;
DO t=2 TO &T;
m{t}=m{t-1};
IF m{t-1}=1 AND RANUNI(seed) > (1-&miss * (t-1))/(1-&miss * (t-2)) THEN m{t} = 0;
IF m{t}=0 THEN y_score{t}=.;
END;
KEEP y1-y&T x;
OUTPUT;
END;
RUN;
DATA LinGM;
SET Sim_LinGM;
%DO t = 1 %TO &T;
id = _N_; time=&t-1; y=y&t; x=x; OUTPUT;
%END;
KEEP id time y x;
RUN;
/*Fit two nested models to the data*/
ODS OUTPUT FitStatistics(persist=proc)=fit;
*Model 1: the true model;
TITLE1 'Linear Growth Model with covariate';
PROC MIXED DATA=LinGM NOCLPRINT COVTEST MAXITER=100 METHOD=ML;
CLASS id;
MODEL y = time x x*time /SOLUTION DDFM=BW CHISQ;
RANDOM INTERCEPT time / SUBJECT=id TYPE=UN GCORR;
RUN;
*Model 2: without treatment effects on the rate of change;
PROC MIXED DATA=LinGM NOCLPRINT COVTEST MAXITER=100 METHOD=ML;
CLASS id;
MODEL y = time x /SOLUTION DDFM=BW CHISQ;
RANDOM INTERCEPT time / SUBJECT=id TYPE=UN GCORR;
RUN;
ODS OUTPUT CLOSE;
%MEND LL;
/*The second Macro: POWER*/
/*This Macro calls the first Macro LL for each replication*/
* Calculate power based on R replications;
%MACRO POWER(R,N,T,seed,df);
DATA tempfit;
DO _N_=1 TO 8;
tempfit=_N_;
OUTPUT;
END;
RUN;
%LL(&N,&T,&seed);
DATA fit;
MERGE fit tempfit;
RUN;
DATA allfit;
SET fit;
RUN;
%DO I = 2 %TO &R;
PROC DATASETS LIBRARY=WORK; DELETE fit; RUN; QUIT;
%LL(&N,&T,%eval(&seed+&I*1389));
DATA fit;
MERGE fit tempfit;
RUN;
DATA allfit;
SET allfit fit;
RUN;
DM 'CLEAR LOG';
%END;
DATA allfit;
SET allfit;
IF MOD(_N_,4) ~= 1 THEN DELETE;
KEEP Value;
RUN;
DATA allfit;
SET allfit;
id =INT((_N_-.1)/2)+1;
modelnum = MOD(_N_+1, 2);
RUN;
PROC TRANSPOSE DATA=allfit OUT=allfit prefix=model;
BY id;
ID modelnum;
VAR Value;
RUN;
DATA allfit;
SET allfit;
ss = &N;
diff = model1 - model0;
ind = 1;
IF diff=. THEN DELETE;
IF diff<0 THEN DELETE;
IF diff < CINV(.95, &df) THEN ind = 0;
DROP id _NAME_ model0 model1;
RUN;
PROC MEANS DATA = allfit;
VAR ss ind;
OUTPUT OUT=power mean(ss ind)=ss power;
RUN;
%MEND POWER;
/*The third Macro: POWERCURVE*/
/* This Macro calls the second Macro for each sample size*/
%MACRO powercurve(R, seed, st, end, step, T,df);
%POWER(&R, &st, &T, %eval(&seed+&st), &df);
DATA allpower;
SET power;
RUN;
%LET st = %eval(&st + &step);
%DO %WHILE (&st <= &end);
%POWER(&R, &st, &T, %eval(&seed+&st), &df);
%LET st = %eval(&st + &step);
DATA allpower;
SET allpower power;
RUN;
DM 'CLEAR LOG';
%END;
* Save the results for possible future use;
DATA allpower;
SET allpower;
FILE "power.txt";
PUT ss power;
RUN;
* Plot the power curve;
ODS PDF FILE='power.pdf' NOTOC;
PROC GPLOT DATA = allpower;
SYMBOL I=JOIN;
PLOT power*ss;
RUN;
QUIT;
ODS PDF CLOSE;
%MEND powercurve;
ODS RESULTS OFF;
ODS LISTING CLOSE;
%powercurve(&R,&seed,&start,&end,&step,&T,&df);
ODS RESULTS ON;
ODS LISTING;